<template>
	<view class="message">
		<view class="centent-box">
			<textarea v-model="message" placeholder="请输入聊天信息"></textarea>
			<button type="primary" @click="sendMessage"> 发 送 消 息 </button>
			
			<navigator style=" width: 120rpx; height: 60rpx; border: 1rpx solid red; margin: 0 auto; margin-top: 80rpx;" @click="goMessageBox">聊天窗口</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				message: ""
			}
		},
		onLoad() {
			
		},
		onShow() {
			// /* websocket长链接 */
			// uni.connectSocket({
			// 	url: 'ws://192.168.3.109:8085/websocket/user/'+uni.getStorageSync('userInfo').id,
			// 	success: res => {
			// 		console.log("建立连接", res);
			// 	}
			// })
			// uni.onSocketOpen(function (res) {
			//   console.log('WebSocket建立连接成功！', res);
			// });
			// uni.onSocketMessage(function (res) {
			// 	console.log('服务器返回的消息：', res)
			// })
		},
		methods: {
			// 发送消息
			sendMessage() {
				const data = {
					fromId: uni.getStorageSync('userInfo').id,
					toId: "1546776383826657282",
					type: 0,
					sendType: 0,
					content: this.message,
					createTime: new Date().getTime()
				}
				uni.sendSocketMessage({
					data: JSON.stringify(data)
				})
				this.message = ""
			},
			goMessageBox(){
				uni.navigateTo({
					// url: '/pagesB/im/chat/index?id=' + uni.getStorageSync('userInfo').id + '&peerId=1556593099372036097',
					url: '/pagesB/im/chat/index?id=1558283527343435778&peerId=1558978300161159169',
					success: function(res) {
						res.eventChannel.emit('item', {
							shopId: '1558978300161159169'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.message{
		width: 100vw;
		height: 100vh;		
	}

</style>